#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.IO;
using System.Diagnostics;

namespace Osh
{
	/// <summary>
	/// Summary description for PacketBinaryReader.
	/// </summary>
	public class PacketBinaryReader : Osh.BinaryReader
	{
		/// <summary>
		/// Initializes a new instance of the PacketBinaryReader class based on the supplied packet stream.
		/// </summary>
		/// <param name="packetStream">A packet stream implementing IPacketStream interface.</param>
		public PacketBinaryReader(PacketStream packetStream)
			: base(packetStream)
		{
		}

		public virtual bool MoveToNextPacket()
		{
			if(this.BasePacketStream == null)
				throw new ObjectDisposedException(null);
			return this.BasePacketStream.MoveToNextPacket();
		}

		public override void Close()
		{
			base.Close();
		}

		public virtual PacketStream BasePacketStream
		{
			get { return (PacketStream)this.BaseStream; }
		}

		public override void SkipBytes(int count)
		{
			this.BasePacketStream.PacketSeek(count, SeekOrigin.Current);
		}

		/*
		public virtual byte[] ReadBytes(int count)
		{
			CheckBuffer(count);

			byte[] result = new byte[count];

			int position = _packetStream.PacketPosition;
			byte[] buffer = _packetStream.PacketBuffer;
			_packetStream.PacketSeek(count, SeekOrigin.Current);
			
			Buffer.BlockCopy(buffer, position, result, 0, count);
			return result;
		}

		public virtual byte ReadByte()
		{
			CheckBuffer(1);
			
			int position = _packetStream.PacketPosition;
			byte[] buffer = _packetStream.PacketBuffer;
			_packetStream.PacketSeek(1, SeekOrigin.Current);

			return buffer[position];
		}

		public virtual ushort ReadUInt16()
		{
			CheckBuffer(2);
			
			int position = _packetStream.PacketPosition;
			byte[] buffer = _packetStream.PacketBuffer;
			_packetStream.PacketSeek(2, SeekOrigin.Current);

			return (ushort)((buffer[position+1] << 8) | buffer[position]);
		}

		public virtual uint ReadUInt32()
		{
			CheckBuffer(4);
			
			int position = _packetStream.PacketPosition;
			byte[] buffer = _packetStream.PacketBuffer;
			_packetStream.PacketSeek(4, SeekOrigin.Current);

			return (uint)((buffer[position + 3] << 24) | (buffer[position + 2] << 16) | 
				(buffer[position + 1] << 8) | buffer[position]);
		}

		private void CheckBuffer(int length)
		{
			if(_packetStream.PacketPosition + length > _packetStream.PacketSize)
			{
				Debug.Assert(false);
				throw new IndexOutOfRangeException();
			}
		}
		*/
	}
}
